'use strict';

angular.module('app')
    .controller('detailsInAndOutController', ['$scope', '$rootScope', '$state', '$timeout', 'toaster','$sce',
        function ($scope, $rootScope, $state, $timeout, toaster,$sce) {
           
            $scope.title = "刷卡详情";
            var id = $state.params.id;
            var inOutType = $state.params.inOutType;
            var date = $state.params.date;
            $scope.projectId = localStorage.getItem('projectId');
            activate(id, inOutType,date);//初始化数据
            Getvideokey();//获取视频的key
            $scope.loading = true;
            $scope.imgss = {};
            $scope.selUser = function () {
                $scope.record.personId = $scope.record.person.split('_')[0];
                $scope.record.cardNo = $scope.record.person.split('_')[1];
            }

            $scope.nearOpen = false;
            $scope.farawayOpen = false;
            $scope.hasVideo = true;
            $scope.myStyle = {
        		'background': 'url(res/img/loading.gif)',
				'background-position': 'center',
				'background-repeat': 'no-repeat'
        	}
            // 初始化页面
            function activate(id, inOutType,date) {
                $.ajax({
                    type: 'PUT',
                    dataType: 'json',
                    contentType: 'application/json;charset=UTF-8',
                    url: 'dac/authm/detail',
                    data: angular.toJson({'id': id, 'inOutType': inOutType, 'date': date})
                }).then(function (result) {
                    $scope.loading = false;
                    if (result.httpCode == 200) {
                        $scope.record = result.data;
                        console.log(result.data);
                    } else {
                        $scope.msg = result.msg;
                        console.log(result.msg);
                    }
                    $scope.$apply();
                });
            }
            //先获取到视频的key，然后才能获取视频以及远程卡门的key来拼接图片地址
            function Getvideokey() {
                $.ajax({
                    type: 'PUT',
                    dataType: 'json',
                    timeout : 30000, //超时时间设置为30s
                    contentType: 'application/json;charset=UTF-8',
                    url: '/dac/authm/loadMSVideo',
                    data: angular.toJson({'id': id,'date': date})
                }).then(function(result) {
                	console.log(result);
                    $scope.myStyle = {};
                    if (result.httpCode == 200) {
                        if(result.data.length>0){
//                      	$timeout(function(){
                        		toaster.clear('*');
                        		toaster.pop('success', '', '视频加载成功');
//                      	},2000)
//                          $scope.hasVideo = true;
                            $scope.key  = result.data[0];//获取到视频key值
                            $scope.videoKey =  $sce.trustAsResourceUrl('http://183.232.65.202:8765/putFile/putVideo?key='+$scope.key);//angular的视频地址转
                            console.log( $scope.videoKey); 
                        }else{
//                          $scope.hasVideo = false;
//							$timeout(function(){
//                         		toaster.clear('*');
//                         		toaster.pop('error', '', '视频不存在或加载失败');
//                      	},2000)
                        }
                       
                    } else {
                        toaster.clear('*');
                        toaster.pop('error', '',result.msg );
                    }
                    
                    //做个友好的判断，如果key值存在就去请求远程开门图片，否则跳出、
                    if( $scope.key &&  $scope.key!=''){
                        GetImgString(id,inOutType,$scope.key,date);//调用获取图片的方法
                    }else{
                        return;
                    }
                    $scope.$apply();
                },function(result){
                	toaster.clear('*');
        			toaster.pop('error', '', '视频不存在或加载失败');
                });
            }
            //获取图片的方法
            function GetImgString(id,inOutType,key,date) {
                console.log(id,inOutType,key);
                if(inOutType=='1' || inOutType=='2'){//开门类型0,1,2,3, 1,2代表远程（app与后台开门），请求的是第一个接口
                    $.ajax({
                        type: 'get',
                        url: 'http://183.232.65.202:8765/putFile/getImgeKey',
                        data: {key:key}
                    }).then(function (result) {
                        $scope.pageInfo=result;
            			$scope.farawayOpen = true;
                        console.log($scope.pageInfo);
                        $scope.$apply();
                    });

                }else if(inOutType=='0' || inOutType=='3') {//室内按键或者刷卡开门
                    $.ajax({
                        type: 'PUT',
                        dataType: 'json',
                        contentType: 'application/json;charset=UTF-8',
                        url: '/dac/authm/loadMSPicture',
                        data: angular.toJson({'id': id,'date': date})
                    }).then(function(result) {
                        if (result.httpCode == 200) {
                            $scope.result = result;
                            $scope.nearOpen = true;
                            console.log(result);
                        } else {
                            toaster.clear('*');
                            toaster.pop('error','',result.msg);
                        }
                    });
                }else {
					//以上两种情况都不符合就跳出
                    return;
                }

            }

        }]);



